package com.wsc.slideoutmenu;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.FrameLayout;

import com.wsc.slideoutmenu.enums.SlideType;
import com.wsc.slideoutmenu.listeners.CustomAnimationListener;

@SuppressWarnings("deprecation")
public class BaseFragment extends FragmentActivity implements OnTouchListener {
	private static final int INFELICITY = 10;
	private static final int GAP = 80;

	private FrameLayout leftContainer;
	private LayoutParams initParams = null;
	private SlideType sideType = SlideType.HIDE;

	private float xTouch = -1;
	private int xLast;

	public void clickMainButton(View view) {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_base);

		leftContainer = (FrameLayout) findViewById(R.id.leftContainer);
		findViewById(R.id.base_fragment).setOnTouchListener(this);

		Rect rectgle = new Rect();
		getWindow().getDecorView().getWindowVisibleDisplayFrame(rectgle);
		initParams = new LayoutParams(rectgle.right - GAP, rectgle.bottom, -(rectgle.right - GAP), 0);
		leftContainer.setLayoutParams(initParams);

		xLast = -initParams.width;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (xTouch == -1)
			xTouch = event.getX();

		if (Math.abs(xTouch - event.getX()) < INFELICITY) {
			Log.d("TAG", "INFELICITY");
			return true;
		}

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			xTouch = event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			int x;
			if (sideType == SlideType.HIDE) {
				// TO RIGTH
				x = (int) (initParams.x + event.getX() - xTouch);
				if (x > xLast && x < 0) {
					xLast = x;
					leftContainer.setLayoutParams(new LayoutParams(initParams.width, initParams.height, (int) xLast, 0));
				}
			} else {
				if (sideType == SlideType.OPEN) {
					// TO LEFT
					x = (int) (event.getX() - xTouch);
					if (x < xLast) {
						xLast = x;
						leftContainer.setLayoutParams(new LayoutParams(initParams.width, initParams.height, (int) xLast, 0));
					}

				}
			}

			Log.d("TAG", "ACTION_MOVE xLast " + xLast);
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			if (sideType == SlideType.HIDE) {
				if ((event.getX() - xTouch) < initParams.width / 4) {
					leftContainer.startAnimation(getAnimation(400, -(initParams.width - xLast), SlideType.HIDE, -initParams.width));
					xLast = -initParams.width;
				} else {
					leftContainer.startAnimation(getAnimation(1000, -xLast, SlideType.OPEN, 0));
					xLast = 0;
				}

			} else {
				if (sideType == SlideType.OPEN) {
					leftContainer.startAnimation(getAnimation(1000, -(initParams.width - xLast), SlideType.HIDE, -initParams.width));
					xLast = -initParams.width;
				}
			}
			break;
		}
		return true;
	}

	private TranslateAnimation getAnimation(long duration, float xDistance, final SlideType resultType, final int rerultXValue) {
		findViewById(R.id.base_fragment).setOnTouchListener(null);
		TranslateAnimation translate = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, xDistance, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0);
		translate.setDuration(duration);
		translate.reset();
		translate.setFillAfter(true);
		translate.setAnimationListener(new CustomAnimationListener() {
			@Override
			public void onAnimationEnd(Animation animation) {
				sideType = resultType;
				Log.d("TAG", resultType.name());
				leftContainer.clearAnimation();
				leftContainer.setLayoutParams(new LayoutParams(initParams.width, initParams.height, rerultXValue, 0));
				findViewById(R.id.base_fragment).setOnTouchListener(BaseFragment.this);
			}
		});

		return translate;
	}
}
